<?php
namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Request;

class Info extends Controller
{
    public function show(){
        $search = Request::instance()->post('search');

        $page = input('page',1);
        $size = '4';
        $count = Db::execute("select * from student");
        $last = ceil($count/$size);
        $offset = ($page - 1) * $size;
        $up = $page - 1 < 1 ?1 :$page-1;
        $down = $page + 1 >$last ? $last : $page + 1;

        $data = Db::query("select * from student where s_name like '%".$search."%' limit $offset,$size");

        $page = array(
            'up' => $up,
            'down' => $down,
            'last' => $last
        );
        if(Request::instance()->isAjax()){
            echo json_encode(array('data'=>$data,'page'=>$page));
        }else{
            return $this -> fetch("show",array('data'=>$data,'page'=>$page));
        }

    }
    public function del(){
        $del_id = Request::instance()->post('del_id');
        $res = Db::execute("delete from student where id = '".$del_id."'");
        if($res){
            echo 1;
        }else{
            echo 0;
        }
    }
    public function upd(){
        $upd_id = Request::instance()->post('upd_id');
        $new_name = Request::instance()->post('new_name');
        $res =Db::execute("update student set s_name = '".$new_name."' where id = '".$upd_id."'");
        if($res){
            echo 1;
        }else{
            echo 0;
        }
    }

}
